//
//  WXRetweetView.swift
//  weibo
//
//  Created by 王旭 on 16/4/21.
//  Copyright © 2016年 王旭. All rights reserved.
//

import UIKit
import SnapKit
import YYText

class WXRetweetView: UIView {
    
    //顶部控件约束
    private var bottomConstraint : Constraint?
    
    var  statusViewModel :WXStatusViewModel?{
        didSet{
            //给文本Label传递数据
            contentLabel.attributedText = statusViewModel?.originalAttributedString
//            contentLabel.text = statusViewModel?.retweetText
//            contentLabel.text = statusViewModel?.status?.text
            
            //给配图传递数据
              //卸载约束
            bottomConstraint?.uninstall()
            if let pic_urls = statusViewModel?.status?.retweeted_status?.pic_urls where pic_urls.count > 0 {
                
                //是否是一张图片
//                if pic_urls.count == 1 {
//                    let layout = photoView.collectionViewLayout as! UICollectionViewFlowLayout
//                    layout.itemSize = statusViewModel!.retweetedPhotoSize
//                }else{
//                    let layout = photoView.collectionViewLayout as! UICollectionViewFlowLayout
//                    layout.itemSize = CGSize(width: PhotoWH, height: PhotoWH)
//                }
                
                //有配图
//                //1.传递数据
//                photoView.pic_urls = pic_urls
//                //2.更新约束
//                photoView.snp_updateConstraints(closure: { (make) -> Void in
//                    make.size.equalTo(statusViewModel!.retweetedPhotoSize)
//                })
                
                photoView.dataInfo = (pic_urls,statusViewModel!.retweetedPhotoSize)

                //3.显示配图
                photoView.hidden = false

                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    bottomConstraint = make.bottom.equalTo(photoView).offset(HomeCellMargin).constraint
                })
            }else{
                //无配图
                //1.更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    bottomConstraint = make.bottom.equalTo(contentLabel).offset(HomeCellMargin).constraint
                })
                //2.隐藏配图
                photoView.hidden = true
            }
//
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
//        backgroundColor = UIColor(white: 180/255, alpha: 1)
        
        //添加控件
        addSubview(contentLabel)
        addSubview(photoView)
        
        //做约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.leading.equalTo(self).offset(HomeCellMargin)
        }
        
        photoView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(HomeCellMargin)
            //固定大小
            make.size.equalTo(CGSizeMake(80, 80))
        }
       self.snp_makeConstraints { (make) -> Void in
            bottomConstraint = make.bottom.equalTo(photoView).offset(HomeCellMargin).constraint
        }
        
    }
    
    //懒加载
    //文本label
    private lazy var contentLabel : YYLabel = {
//        let contentLabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: ContentFontSize, preferredMaxLayoutWidth: SCREEN_W - 2*HomeCellMargin)
        let contentLabel = YYLabel()
        contentLabel.userInteractionEnabled = true
        contentLabel.textColor = UIColor.darkGrayColor()
        contentLabel.font = UIFont.systemFontOfSize(ContentFontSize)
        contentLabel.preferredMaxLayoutWidth = SCREEN_W - 2 * HomeCellMargin
        contentLabel.numberOfLines = 0
        return contentLabel
    }()
    //配图
    private lazy var photoView : WXPhotoCollectionView = WXPhotoCollectionView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
}
